home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / pearcmd.php < prev    next >
PHP Script  |  2004-12-20  |  10KB  |  303 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: pearcmd.php,v 1.10 2004/03/13 17:37:23 cellog Exp $
  22.  
  23. ob_end_clean();
  24. /**
  25.  * @nodep Gtk
  26.  */
  27. if ('\xampp\php\pear' != '@'.'include_path'.'@') {
  28.     ini_set('include_path', '\xampp\php\pear');
  29. }
  30. ini_set('allow_url_fopen', true);
  31. if (!ini_get('safe_mode')) {
  32.     set_time_limit(0);
  33. }
  34. ob_implicit_flush(true);
  35. ini_set('track_errors', true);
  36. ini_set('html_errors', false);
  37. ini_set('magic_quotes_runtime', false);
  38. set_error_handler('error_handler');
  39.  
  40. $pear_package_version = "1.3.1";
  41.  
  42. require_once 'PEAR.php';
  43. require_once 'PEAR/Config.php';
  44. require_once 'PEAR/Command.php';
  45. require_once 'Console/Getopt.php';
  46.  
  47. PEAR_Command::setFrontendType('CLI');
  48. $all_commands = PEAR_Command::getCommands();
  49.  
  50. $argv = Console_Getopt::readPHPArgv();
  51. /* $progname = basename($argv[0]); */
  52. $progname = 'pear';
  53. array_shift($argv);
  54. $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
  55. if (PEAR::isError($options)) {
  56.     usage($options);
  57. }
  58.  
  59. $opts = $options[0];
  60.  
  61. $fetype = 'CLI';
  62. if ($progname == 'gpear' || $progname == 'pear-gtk') {
  63.     $fetype = 'Gtk';
  64. } else {
  65.     foreach ($opts as $opt) {
  66.         if ($opt[0] == 'G') {
  67.             $fetype = 'Gtk';
  68.         }
  69.     }
  70. }
  71. PEAR_Command::setFrontendType($fetype);
  72. $ui = &PEAR_Command::getFrontendObject();
  73. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  74.  
  75. $pear_user_config = '';
  76. $pear_system_config = '';
  77. $store_user_config = false;
  78. $store_system_config = false;
  79. $verbose = 1;
  80.  
  81. foreach ($opts as $opt) {
  82.     switch ($opt[0]) {
  83.         case 'c':
  84.             $pear_user_config = $opt[1];
  85.             break;
  86.         case 'C':
  87.             $pear_system_config = $opt[1];
  88.             break;
  89.     }
  90. }
  91.  
  92. $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
  93. $verbose = $config->get("verbose");
  94. $cmdopts = array();
  95.  
  96. foreach ($opts as $opt) {
  97.     $param = !empty($opt[1]) ? $opt[1] : true;
  98.     switch ($opt[0]) {
  99.         case 'd':
  100.             list($key, $value) = explode('=', $param);
  101.             $config->set($key, $value, 'user');
  102.             break;
  103.         case 'D':
  104.             list($key, $value) = explode('=', $param);
  105.             $config->set($key, $value, 'system');
  106.             break;
  107.         case 's':
  108.             $store_user_config = true;
  109.             break;
  110.         case 'S':
  111.             $store_system_config = true;
  112.             break;
  113.         case 'u':
  114.             $config->remove($param, 'user');
  115.             break;
  116.         case 'v':
  117.             $config->set('verbose', $config->get('verbose') + 1);
  118.             break;
  119.         case 'q':
  120.             $config->set('verbose', $config->get('verbose') - 1);
  121.             break;
  122.         case 'V':
  123.             usage(null, 'version');
  124.         default:
  125.             // all non pear params goes to the command
  126.             $cmdopts[$opt[0]] = $param;
  127.             break;
  128.     }
  129. }
  130.  
  131. if ($store_system_config) {
  132.     $config->store('system');
  133. }
  134.  
  135. if ($store_user_config) {
  136.     $config->store('user');
  137. }
  138.  
  139. $command = (isset($options[1][0])) ? $options[1][0] : null;
  140.  
  141. if (empty($command) && ($store_user_config || $store_system_config)) {
  142.     exit;
  143. }
  144.  
  145. if ($fetype == 'Gtk') {
  146.     Gtk::main();
  147. } else do {
  148.     if ($command == 'help') {
  149.         usage(null, @$options[1][1]);
  150.     }
  151.  
  152.     PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  153.     $cmd = PEAR_Command::factory($command, $config);
  154.     PEAR::popErrorHandling();
  155.     if (PEAR::isError($cmd)) {
  156.         usage(null, @$options[1][1]);
  157.     }
  158.  
  159.     $short_args = $long_args = null;
  160.     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  161.     array_shift($options[1]);
  162.     if (PEAR::isError($tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args))) {
  163.         break;
  164.     }
  165.     list($tmpopt, $params) = $tmp;
  166.     $opts = array();
  167.     foreach ($tmpopt as $foo => $tmp2) {
  168.         list($opt, $value) = $tmp2;
  169.         if ($value === null) {
  170.             $value = true; // options without args
  171.         }
  172.         if (strlen($opt) == 1) {
  173.             $cmdoptions = $cmd->getOptions($command);
  174.             foreach ($cmdoptions as $o => $d) {
  175.                 if (@$d['shortopt'] == $opt) {
  176.                     $opts[$o] = $value;
  177.                 }
  178.             }
  179.         } else {
  180.             if (substr($opt, 0, 2) == '--') {
  181.                 $opts[substr($opt, 2)] = $value;
  182.             }
  183.         }
  184.     }
  185.     $ok = $cmd->run($command, $opts, $params);
  186.     if ($ok === false) {
  187.         PEAR::raiseError("unknown command `$command'");
  188.     }
  189. } while (false);
  190.  
  191. // {{{ usage()
  192.  
  193. function usage($error = null, $helpsubject = null)
  194. {
  195.     global $progname, $all_commands;
  196.     $stderr = fopen('php://stderr', 'w');
  197.     if (PEAR::isError($error)) {
  198.         fputs($stderr, $error->getMessage() . "\n");
  199.     } elseif ($error !== null) {
  200.         fputs($stderr, "$error\n");
  201.     }
  202.     if ($helpsubject != null) {
  203.         $put = cmdHelp($helpsubject);
  204.     } else {
  205.         $put =
  206.             "Usage: $progname [options] command [command-options] <parameters>\n".
  207.             "Type \"$progname help options\" to list all options.\n".
  208.             "Type \"$progname help <command>\" to get the help for the specified command.\n".
  209.             "Commands:\n";
  210.         $maxlen = max(array_map("strlen", $all_commands));
  211.         $formatstr = "%-{$maxlen}s  %s\n";
  212.         ksort($all_commands);
  213.         foreach ($all_commands as $cmd => $class) {
  214.             $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  215.         }
  216.     }
  217.     fputs($stderr, "$put\n");
  218.     fclose($stderr);
  219.     exit;
  220. }
  221.  
  222. function cmdHelp($command)
  223. {
  224.     global $progname, $all_commands, $config;
  225.     if ($command == "options") {
  226.         return
  227.         "Options:\n".
  228.         "     -v         increase verbosity level (default 1)\n".
  229.         "     -q         be quiet, decrease verbosity level\n".
  230.         "     -c file    find user configuration in `file'\n".
  231.         "     -C file    find system configuration in `file'\n".
  232.         "     -d foo=bar set user config variable `foo' to `bar'\n".
  233.         "     -D foo=bar set system config variable `foo' to `bar'\n".
  234.         "     -G         start in graphical (Gtk) mode\n".
  235.         "     -s         store user configuration\n".
  236.         "     -S         store system configuration\n".
  237.         "     -u foo     unset `foo' in the user configuration\n".
  238.         "     -h, -?     display help/usage (this message)\n".
  239.         "     -V         version information\n";
  240.     } elseif ($command == "shortcuts") {
  241.         $sc = PEAR_Command::getShortcuts();
  242.         $ret = "Shortcuts:\n";
  243.         foreach ($sc as $s => $c) {
  244.             $ret .= sprintf("     %-8s %s\n", $s, $c);
  245.         }
  246.         return $ret;
  247.  
  248.     } elseif ($command == "version") {
  249.         return "PEAR Version: ".$GLOBALS['pear_package_version'].
  250.                "\nPHP Version: ".phpversion().
  251.                "\nZend Engine Version: ".zend_version().
  252.                "\nRunning on: ".php_uname();
  253.  
  254.     } elseif ($help = PEAR_Command::getHelp($command)) {
  255.         if (is_string($help)) {
  256.             return "$progname $command [options] $help\n";
  257.         }
  258.         if ($help[1] === null) {
  259.             return "$progname $command $help[0]";
  260.         } else {
  261.             return "$progname $command [options] $help[0]\n$help[1]";
  262.         }
  263.     }
  264.     return "No such command";
  265. }
  266.  
  267. // }}}
  268.  
  269. function error_handler($errno, $errmsg, $file, $line, $vars) {
  270.     if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
  271.         return; // @silenced error
  272.     }
  273.     $errortype = array (
  274.         E_ERROR   =>  "Error",
  275.         E_WARNING   =>  "Warning",
  276.         E_PARSE   =>  "Parsing Error",
  277.         E_NOTICE   =>  "Notice",
  278.         E_CORE_ERROR  =>  "Core Error",
  279.         E_CORE_WARNING  =>  "Core Warning",
  280.         E_COMPILE_ERROR  =>  "Compile Error",
  281.         E_COMPILE_WARNING =>  "Compile Warning",
  282.         E_USER_ERROR =>  "User Error",
  283.         E_USER_WARNING =>  "User Warning",
  284.         E_USER_NOTICE =>  "User Notice"
  285.     );
  286.     $prefix = $errortype[$errno];
  287.     $file = basename($file);
  288.     print "\n$prefix: $errmsg in $file on line $line\n";
  289. }
  290.  
  291.  
  292. /*
  293.  * Local variables:
  294.  * tab-width: 4
  295.  * c-basic-offset: 4
  296.  * indent-tabs-mode: nil
  297.  * mode: php
  298.  * End:
  299.  */
  300. // vim600:syn=php
  301.  
  302. ?>
  303.